<template>
    <div class="common-page page-system-article-of-tags" v-loading.fullscreen="loading">
        <div class="common-page__head">
            <!-- <comp-paths class="route" :paths="['/system', $route.fullPath]"></comp-paths> -->
            <div class="title">{{ $options.title }}</div>
        </div>
        <comp-article-of-tags class="common-page__body" ref="list" expanded select link>
            <template #head="scope">
                <el-button type="plain" size="small" icon="el-icon-refresh" @click="search()">刷新列表</el-button>
                <el-button type="danger" size="small" icon="el-icon-delete" @click="delTag(...scope.selects)">删除分类</el-button>
            </template>
            <template #body="scope">
                <el-button type="plain" size="mini" @click.stop="addTag(scope.data)" v-if="scope.data.level === 1">添加子分类</el-button>
                <el-button type="primary" size="mini" @click.stop="setTag('start', scope.data)" v-if="scope.data.level > 1">编辑</el-button>
                <el-button type="danger" size="mini" @click.stop="delTag(scope.data)" v-if="scope.data.level > 1">删除</el-button>
            </template>
        </comp-article-of-tags>
        <comp-dialog :title="tag && tag.id ? '编辑分类' : '添加分类'" width="380px" ref="tag">
            <el-form size="small" label-width="98px" @submit.native.prevent v-if="tag">
                <el-form-item label="父级分类：" v-if="tag.level > 1">
                    <span>{{ tag.tag.name }}</span>
                </el-form-item>
                <el-form-item label="分类名称：" required>
                    <el-input ref="tagName" v-model="tag.name" style="width: 240px" placeholder="请输入" :maxlength="20" :readonly="loading" @change="setTag('validate')"></el-input>
                </el-form-item>
                <el-form-item label="排序值：">
                    <el-input v-model="tag.sort" style="width: 160px" placeholder="请输入" :readonly="loading" @change="setTag('validate')"></el-input>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" icon="el-icon-check" @click="setTag('submit')">{{ tag.id ? "保存" : "添加" }}</el-button>
                    <el-button type="plain" icon="el-icon-close" @click="setTag('end')">放弃</el-button>
                </el-form-item>
            </el-form>
        </comp-dialog>
    </div>
</template>

<script>
    import mixin from "../mixin";
    import { string, int, deepCopy } from "../util";
    export default {
        name: "page-system-article-of-tags",
        title: "信息分类管理",
        nodes: ["/menu/system"],
        childs: [],
        mixins: [mixin],
        data: () => ({
            loading: false,
            tag: null,
        }),
        methods: {
            search() {
                if (this.loading) return;
                this.$refs.list.init();
            },
            addTag(tag) {
                if (this.loading) return;
                let target = this.plugins.user.formatArticleOfTag();
                if (tag) {
                    target.tag.id = tag.id;
                    target.tag.name = tag.name;
                    target.level = tag.level + 1;
                    target.sort = 100 - tag.tags.length;
                } else {
                    target.level = 1;
                }
                this.setTag("start", target);
            },
            setTag(action, data) {
                if (this.loading) return;
                let form = this.tag;
                if (!action || action === "start") {
                    this.tag = deepCopy(data);
                    this.setTag("validate");
                    this.$refs.tag.init();
                    this.$nextTick(() => this.$refs.tagName.focus());
                } else if (action === "validate") {
                    let error = "";
                    form.name = string(form.name).trim();
                    error = error || (form.name ? "" : "请输入分类名称！");
                    form.sort = Math.max(int(form.sort), 0) || null;
                    if (data) this.plugins.modal.toast(error, "error");
                    return !error;
                } else if (action === "submit") {
                    if (!this.setTag("validate", true)) return;
                    else this.loading = true;
                    if (form.id) this.plugins.user.setArticleOfTag(form).then((res) => {
                        this.loading = false;
                        if (res.code === 0) {
                            this.plugins.modal.toast("保存成功！", "success");
                            this.setTag("end");
                            this.search();
                        } else {
                            this.plugins.modal.alert("保存失败", res.code + "：" + res.msg, "error");
                        }
                    });
                    else this.plugins.user.addArticleOfTag(form).then((res) => {
                        this.loading = false;
                        if (res.code === 0) {
                            this.plugins.modal.toast("添加成功！", "success");
                            this.setTag("end");
                            this.search();
                        } else {
                            this.plugins.modal.alert("添加失败", res.code + "：" + res.msg, "error");
                        }
                    });
                } else if (action === "end") {
                    this.$refs.tag.close();
                }
            },
            delTag(...tags) {
                if (this.loading) return;
                if (tags.length === 0) return this.plugins.modal.toast("请先勾选要删除的分类哦！", "warning");
                let content = " <span style='color:red;'>" + (tags.length > 1 ? "勾选的" + tags.length + "个分类" : tags[0].name) + "</span> 吗？";
                this.plugins.modal.alert("删除确认", "确定删除" + content, "warning", ["确定删除", "再想想"]).then((ok) => {
                    if (!ok) return;
                    this.loading = true;
                    this.plugins.user.delArticleOfTag(...tags).then((res) => {
                        this.loading = false;
                        if (res.code === 0) {
                            this.plugins.modal.toast("删除成功！", "success");
                            this.search();
                        } else {
                            this.plugins.modal.alert("删除失败", res.code + "：" + res.msg, "error");
                        }
                    });
                });
            },
            destroy() {
                if (this.loading) return;
                return true;
            },
            init() {
                if (this.loading) return;
                this.loading = true;
                this.$root.$refs.navbar.init();
                this.plugins.user.call(async () => {
                    let res = await this.plugins.user.init();
                    if (res.code === 0) { }
                    return res;
                }, async () => {
                    if (this.access(this.$route.name)) return { code: 0 };
                    return this.env.xhrErrors.find((error) => error.code === 4);
                }).then((res) => {
                    this.loading = false;
                    this.$root.$refs.navbar.init();
                    if (res.code === 0) {
                        this.search();
                    } else if (res.code === 2) {
                        this.$root.$refs.navbar.login();
                    } else {
                        this.plugins.modal.alert("页面加载失败", res.code + "：" + res.msg, "error").then(() => this.$router.replace("/home"));
                    }
                });
            },
        },
    };
</script>

<style lang="scss">
    .page-system-article-of-tags {
        overflow: hidden;
    }
</style>